<?php
// 启动会话
session_start();

// 数据库连接配置
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'test');
define('DB_NAME', 'test');

// 错误报告设置
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 设置时区
date_default_timezone_set('Asia/Shanghai');

// 数据库连接函数
function getDBConnection() {
    try {
        $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4";
        $pdo = new PDO($dsn, DB_USER, DB_PASS);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdo;
    } catch(PDOException $e) {
        die("连接失败: " . $e->getMessage());
    }
}

// 获取当前页面名称
$current_page = basename($_SERVER['PHP_SELF']);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的PHP网站</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <!-- 自定义CSS -->
    <style>
        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            height: 60px;
            line-height: 60px;
            background-color: #f5f5f5;
        }
        .main-content {
            margin-bottom: 80px;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="index.php">我的网站</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link <?php echo ($current_page == 'index.php') ? 'active' : ''; ?>" 
                           href="index.php">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link <?php echo ($current_page == 'about.php') ? 'active' : ''; ?>" 
                           href="about.php">关于</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link <?php echo ($current_page == 'contact.php') ? 'active' : ''; ?>" 
                           href="contact.php">联系我们</a>
                    </li>
                </ul>
                <ul class="navbar-nav ms-auto">
                    <?php if(isset($_SESSION['user_id'])): ?>
                        <li class="nav-item">
                            <a class="nav-link" href="profile.php">个人中心</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="logout.php">退出</a>
                        </li>
                    <?php else: ?>
                        <li class="nav-item">
                            <a class="nav-link" href="login.php">登录</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="register.php">注册</a>
                        </li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容区 -->
    <div class="container main-content">
        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h2>欢迎来到我的网站</h2>
                    </div>
                    <div class="card-body">
                        <?php
                        // 获取当前时间
                        $current_time = date('Y-m-d H:i:s');
                        
                        // 显示欢迎信息
                        if(isset($_SESSION['user_id'])) {
                            echo "<h5>欢迎回来，" . htmlspecialchars($_SESSION['username']) . "！</h5>";
                        } else {
                            echo "<h5>欢迎访客！</h5>";
                        }
                        echo "<p>当前时间：{$current_time}</p>";
                        ?>

                        <!-- 示例：最新文章列表 -->
                        <h4 class="mt-4">最新文章</h4>
                        <?php
                        try {
                            $pdo = getDBConnection();
                            $stmt = $pdo->query("SELECT * FROM student");
                            if($stmt->rowCount() > 0) {
                                echo "<ul class='list-group'>";
                                while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                    echo "<li class='list-group-item'>";
                                    echo htmlspecialchars($row['name']);
                                    echo " <small class='text-muted'>- " . $row['major'] . "</small>";
                                    echo "</li>";
                                }
                                echo "</ul>";
                            } else {
                                echo "<p>暂无文章</p>";
                            }
                        } catch(PDOException $e) {
                            echo "<p class='text-danger'>获取文章失败: " . $e->getMessage() . "</p>";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <span class="text-muted">© <?php echo date('Y'); ?> 我的PHP网站. 保留所有权利.</span>
        </div>
    </footer>

    <!-- Bootstrap JS -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>
</body>
</html>